<?php
set_include_path ( $_SERVER ['DOCUMENT_ROOT'] );
include_once 'admin/includes/functions/admin-functions.php';
global $DBobject, $SMARTY;

$error = "登陆状态已经过期，请刷新重试。";
$referer = parse_url($_SERVER['HTTP_REFERER']);
if( $referer['host'] == $GLOBALS['HTTP_HOST'] && checkToken('admin', $_POST["formToken"])){
	$response = false;
	$url = false;
	
	switch ($_POST['action']) {
		case 'FindMember':
			$member_obj = new Member();
			$franchises = ($_SESSION['user']['admin']['level'] > 1)?$_SESSION['user']['admin']['franchisee']:"";
			$members = $member_obj->FindMembers($_POST['query'], $franchises);
			$SMARTY->assign('query', $_POST['query']);
			$SMARTY->assign('members', $members);
			$template = $SMARTY->fetch('found-members.tpl');
			echo json_encode(array(
					"error" => null,
					"success" => true,
					'template' => $template,
					'timestamp'=>$_POST['timestamp']
			));
			exit;
			
		case 'UpdateMember':
			$error = null;
			$member_obj = new Member();
			$res1 = $member_obj->UpdateDetails($_POST['user']);
			if($res1['error']) {
				$error = $res1['error'];
				break;
			}
			$_POST['address']['B']['address_user_id'] = $_POST['user']['user_id'];
			$_POST['address']['B']['address_name'] = $_POST['user']['user_gname'];
			$_POST['address']['B']['address_surname'] = $_POST['user']['user_surname'];
			$addressID = $member_obj->InsertNewAddress($_POST['address']['B']);
			if(empty($addressID)){
				$error = "Address can't be updated.";
				break;
			}
			echo json_encode(array(
					"error" => null,
					"success" => true
			));
			exit;

		case 'UpdateWarning':
			$error = null;
			$member_obj = new Member();
			$res = $member_obj->UpdateMemberWarning($_POST['user_id'], $_POST['user_warning']);
			if($res['error']) {
				$error = $res['error'];
				break;
			}
			echo json_encode(array(
					"error" => null,
					"success" => $res['success'],
					"warning" => $_POST['user_warning']
			));
			exit;
				
		case 'DeleteMember':
			$error = null;
			$member_obj = new Member();
			$enrol_obj = new Enrolment();
			$res1 = $member_obj->DeleteUSer($_POST['user_id']);
			$students = $member_obj->GetStudentsByUserId($_POST['user_id']);
			foreach ($students as $student){
				$enrol_obj->DeleteStudentEnrolments($student['student_id']);
				$member_obj->DeleteStudent($student['student_id']);
			}
			if($res1['error']) {
				$error = $res1['error'];
				break;
			}
			unset($_SESSION['existing']);
			echo json_encode(array(
					"error" => null,
					"success" => $res1['success']
			));
			exit;
				
	
		case 'DeleteStudent':
			$error = null;
			$member_obj = new Member();
			$enrol_obj = new Enrolment();
			foreach( $_POST['student'] as $student){
				$enrol_obj->DeleteStudentEnrolments($student['student_id']);
				$member_obj->DeleteStudent($student['student_id']);
			}
			echo json_encode(array(
					"error" => null,
					"success" => true
			));
			exit;
				
		case 'UpdateStudent':
			$error = null;
			$member_obj = new Member();
			foreach( $_POST['student'] as $student){
				$member_obj->EditStudent($student);
			}
			echo json_encode(array(
					"error" => null,
					"success" => true
			));
			exit;
			
		case 'SetExistingMember':
			$member_obj = new Member();
			$memberArr = $member_obj->GetMemberDetails($_POST['user_id']);
			$_SESSION['existing']['user'] = $memberArr['user'];
			$SMARTY->assign('user', $memberArr['user']);
			$SMARTY->assign('notes', $memberArr['notes']);
			$SMARTY->assign('emails', $memberArr['emails']);
			$address = array('B'=>$memberArr['address']);
			$SMARTY->assign('address', $address);
			$enrol_obj = new Enrolment();
			$studentArr = array();
			foreach ($memberArr['students'] as $student){
				$studentArr["{$student['student_id']}"] = $student;
				$studentArr["{$student['student_id']}"]['enrolments'] = $enrol_obj->GetStudentEnrolments($student['student_id']);
			}
			$SMARTY->assign('students', $studentArr);
			$payments = $enrol_obj->GetOrderHistoryByUser($_POST['user_id']);
			$SMARTY->assign ( 'payments', $payments );
			
			$classTermObj = new ClassTermClass();
			$terms = $classTermObj->GetCurrentTerm();
			$SMARTY->assign("terms",$terms);
			$classes = $classTermObj->GetCurrentClasses();
			$SMARTY->assign("classes",$classes);
			
			$token = getToken('admin');
			$SMARTY->assign('token',$token);
			$template = $SMARTY->fetch('member-details.tpl');
			echo json_encode(array(
					"error" => null,
					"success" => true,
					'template' => $template,
					"debug"=>$studentArr
			));
			exit;

		case 'AddNewNote':
			$error = null;
			$success = 'The note was successfully added.';
			$member_obj = new Member();
			$response = $member_obj->AddNotes($_SESSION['user']['admin']["id"], $_POST['user_id'], $_POST['notes']);
			$SMARTY->assign('notes', $member_obj->GetMemberNotes($_POST['user_id']));
			$template = $SMARTY->fetch('table-notes.tpl');
			
			if(empty($response)){
				$error = 'The note cannot be added.';
				$success = null;
			}
			echo json_encode(array(
					"error" => $error,
					"success" => $success,
					"template" => $template
			));
			exit;
		
	}
}
echo json_encode(array("error"=>$error));
die();









